<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Gclient extends CI_Controller {
function __construct()
    {
        parent::__construct();
    	$this->load->library('session');
		$this->load->helper('url');
    	$this->load->database();
    	$this->load->model("centermod");
    	
	}

/*******************  Page   *********************************/
function clientform()
	{
		if($this->session->userdata("user")==false)
			{
					header('Location:index.php?pages/loginadmin');
						
			}else 
			{
				$data=array("type"=>"add");
				$this->load->view("client_form",$data);
				
			}	
	}

	
// get edit form 
	function Editclientform($idclient)
	{
		if($this->session->userdata("user")==false)
			{
					header('Location:index.php?pages/loginadmin');
						
			}else 
			{
		
				$infoclient=$this->centermod->getclientinfobyid($idclient);
				$data=array("infoclient"=>$infoclient,"type"=>"edit");
				$this->load->view("client_form",$data);
	
			}	
		}	
/*#############################  end pages   ##############################*/

	

	
	
/*******************       add    *********************************/

function addclientinfo()
	{
			
			if($this->session->userdata("user")==false && $this->session->userdata("id")<=0)
			{
					header('Location:index.php?pages/loginadmin');
						
			}else 
			{
		
			$companyname=$this->input->get_post("companyname");
			$address=$this->input->get_post("address");
			$tele=$this->input->get_post("tele");
			$city=$this->input->get_post("city");
			$fax=$this->input->get_post("fax");
			$contactperson=$this->input->get_post("contactperson");
			$contactpersonemail=$this->input->get_post("contactpersonemail");
			  
      		$data=array(    "companyname"=>$companyname,
      						"address"=>$address,
      						"tele"=>$tele,
							"city"=>$city,
      						"fax"=>$fax,
      						"contactperson"=>$contactperson,
      						"contactpersonemail"=>$contactpersonemail
      					);
			if($companyname !="" && $address!="" && $tele!="")
				{
					$idinsert=$this->centermod-> insertetemplate("clients",$data);
					if($idinsert >0)
						{
								$iduser=$this->session->userdata("id");
								$idclient=$idinsert;
								$statusid=1;
								$data_fllowups=array(
														 "User_idUser"=>$iduser,
														 "clients_idclients"=>$idclient,
														 "status_idstatus"=>$statusid
													);
								$idinsert=$this->centermod-> insertetemplate("casefollowups",$data_fllowups);					
								//$this->load->view("listclient");
								//$this->listclient();
								 redirect('index.php?gclient/listclient', 'refresh');
						}else 
						{
							echo "system ne peut pas ajoute l'information de cleint";
						}
					
					
				}else
				{
					echo "name , email, adresse ne peux pas null"; 
				}
				
			
		}
	}	
	
	
	
/*#############################  end  add  #############################*/	
	
	

	
	
/**************************** list **************************************/
	function listclient()
	{
		if($this->session->userdata("user")==false && $this->session->userdata("id")<=0)
			{
					header('Location:index.php?pages/loginadmin');
						
			}else 
			{
				$listallclient=$this->centermod->listallclientbyiduser($this->session->userdata("id"));
				$data=array("listclient"=>$listallclient);
				$this->load->view("listclient",$data);
				
				
			}
	}
	
/*############################end List #################################*/		
	
	
	
	
	
	
/*******************      Edit    *********************************/	
 function  editclient()
 {
 			
 	if($this->session->userdata("user")==false && $this->session->userdata("id")<=0)
			{
					header('Location:index.php?pages/loginadmin');
						
			}else {
					 	$idclient=$this->input->get_post("idclients");
						if($idclient>0)
					 	{
					 		$listallclient=$this->centermod->getclientinfobyid($idclient);
					 			$data=array("type"=>"edit","infoclient"=>$listallclient);
									$this->load->view("client_form",$data);
					 	} 
					 	else
					 	{
					 		echo"note good idclient";
					 	}
					 	
			} 	
  }

function submiteditclient()
{
	if($this->session->userdata("user")==false && $this->session->userdata("id")<=0)
			{
					header('Location:index.php?pages/loginadmin');
						
			}else
			{
			$companyname=$this->input->get_post("companyname");
			$address=$this->input->get_post("address");
			$tele=$this->input->get_post("tele");
			$city=$this->input->get_post("city");
			$fax=$this->input->get_post("fax");
			$contactperson=$this->input->get_post("contactperson");
			$contactpersonemail=$this->input->get_post("contactpersonemail");
			$idclients=$this->input->get_post("idclients");
			$data=array("companyname"=>$companyname,"address"=>$address,"tele"=>$tele,"city"=>$city,"contactperson"=>$contactperson,"contactpersonemail"=>$contactpersonemail);
			if ($idclients!="")
				{
					$this->centermod-> updatetemplate("clients",$data,"idclients =$idclients");
					redirect("index.php?gclient/listclient",'refresh');
				
				}
			}
}	


function editclientstatus()
	{
			if($this->session->userdata("user")==false && $this->session->userdata("id")<=0)
			{
					header('Location:index.php?pages/loginadmin');
						
			}else
			{
				$idcase=$this->input->get_post("idcase");
				$statusinfo=$this->centermod->listestatusclientbyidcase($idcase);
				$this->load->view("client_status.php",$statusinfo);
	
			}
	}
	
//	
	
/*#############################  end  edit  #############################*/	

  
}